package com.example.zsq.myapplication;

import android.content.DialogInterface;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;

public class MainActivity extends AppCompatActivity {
    private Button start = null;
    private Button end   = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        start = (Button)findViewById(R.id.button);
        end   = (Button)findViewById(R.id.button2);
        start.setOnClickListener(new StartButtonListener());
        end.setOnClickListener(new EndButtonListener());
    }
    Handler handler  = new Handler();
     class StartButtonListener implements OnClickListener
     {
         public  void onClick(View v)
         {
             handler.post(update);
         }
     }
     class EndButtonListener implements OnClickListener
     {
         public  void onClick(View v)
         {

             handler.removeCallbacks(update);

         }
     }
     Runnable update = new Runnable() {
         @Override
         public void run() {
              System.out.println("this is a  runable");
             Log.v("tag", "Update");
              handler.postDelayed(update, 2000);
         }
     };
}
